//
//  ModuleFirstUserHomepageViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ModuleFirstUserHomepageViewController.h"
#import "TravelsAuthor _HeadView.h"
#import "TravelsAuthor _TravelsView.h"
#import "TravelsAuthor_LoveView.h"


#import "TravelsDetailsController.h"
#import "FirstPhotoDetailsViewController.h"

#import "ModuleFirst_UserHomePageModel.h"
#import "ModuleFirst_UserTripsModel.h"
#import "ModuleFirst_UserLoveModel.h"


#define ViewW self.view.frame.size.width
#define ViewH self.view.frame.size.height

@interface ModuleFirstUserHomepageViewController ()

@property (nonatomic, strong) TravelsAuthor__HeadView *headView;

@property (nonatomic, strong) UIButton *travelsButton;

@property (nonatomic, strong) UIButton *loveButton;

@property (nonatomic, strong) TravelsAuthor__TravelsView *TA_TravelsView;

@property (nonatomic, strong) TravelsAuthor_LoveView *TA_LoveView;

@property (nonatomic, strong) NSMutableArray *allTripsArray;

@property (nonatomic, strong) NSMutableArray *allLoveArray;

@property (nonatomic, strong) ModuleFirst_UserHomePageModel *bigModel;


@end

@implementation ModuleFirstUserHomepageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    //根据传进来的作者名该
    self.navigationItem.title = self.userModel.name;
//    NSLog(@"%@", self.userModel.name);
    
    self.allTripsArray = [NSMutableArray array];
    self.allLoveArray = [NSMutableArray array];
    self.bigModel = [[ModuleFirst_UserHomePageModel alloc]init];
    
    
    
    //调用方法,获取作者信息和游记数据
    [self getAFNetworkingTravelsData];
    
    
    
//    //调用方法,获取喜欢数据
//    [self getAFNetworkingLoveData];
    
    //调整坐标
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    //头部固定视图
    self.headView = [[TravelsAuthor__HeadView alloc]initWithFrame:CGRectMake(0, 0, ViewW, ViewH / 5)];
//    self.headView.backgroundColor = [UIColor whiteColor];
    self.headView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    [self.view addSubview:self.headView];
    
    
    
    
    //游记按钮
    self.travelsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.travelsButton.frame = CGRectMake(0, ViewH / 5, ViewW / 2 - 0.5 , ViewH / 5 / 3);
//    self.travelsButton.backgroundColor = [UIColor whiteColor];
    self.travelsButton.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    [self.view addSubview:self.travelsButton];
    
    [self.travelsButton setTitle:@"游记" forState:UIControlStateNormal];
    [self.travelsButton setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
    self.travelsButton.alpha = 0.5;
    [self.travelsButton addTarget:self action:@selector(travelsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //喜欢按钮
    self.loveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loveButton.frame = CGRectMake(ViewW / 2 + 0.5, ViewH / 5, ViewW / 2 - 0.5 , ViewH / 5 / 3);
    self.loveButton.backgroundColor = [UIColor lightTextColor];
    [self.view addSubview:self.loveButton];
    
    [self.loveButton setTitle:@"喜欢" forState:UIControlStateNormal];
    [self.loveButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [self.loveButton addTarget:self action:@selector(loveButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //游记页面
    self.TA_TravelsView = [[TravelsAuthor__TravelsView alloc]initWithFrame:CGRectMake(0, ViewH / 5 + ViewH / 5 / 3  - 10, ViewW, ViewH / 5 * 4 - ViewH / 5 / 3 - 64  + 10)];
    self.TA_TravelsView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    self.TA_TravelsView.UIModel = self.userModel;
    [self.view addSubview:self.TA_TravelsView];
    
#pragma mark---游记页面创建通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toTravelsDetails:) name:@"toTravelsDetails" object:nil];
    
    
    
    //导航栏按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"BackButton_1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBackButton)];
    
}



//(游记)网络请求数据
- (void)getAFNetworkingTravelsData{
    
    //创建一个管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    [manager.securityPolicy setValidatesDomainName:NO];
    //识别各种类型的网址
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    NSString *idd = self.userModel.idd.stringValue;
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/users/%@.json?page=1",idd];
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self.bigModel setValuesForKeysWithDictionary:responseObject];
        
        //给headView传值
        self.headView.bigModel = self.bigModel;
        

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error====%@",error);

        
    }];
    
}


//游记按钮点击方法
- (void)travelsButtonAction:(UIButton *)isButton{
    
    [self.travelsButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.loveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //刷新游记页面上的tableview;
    [self.TA_TravelsView.travelsTableView reloadData];
    [self.view bringSubviewToFront:self.TA_TravelsView];
    
    
}






//喜欢按钮点击方法
- (void)loveButtonAction:(UIButton *)isButton{
    
    //喜欢页面

    self.TA_LoveView = [[TravelsAuthor_LoveView alloc]initWithFrame:CGRectMake(0, ViewH / 5 + ViewH / 5 / 3  + 10, ViewW, ViewH / 5 * 4 - ViewH / 5 / 3 - 10)];
    self.TA_LoveView.backgroundColor = [UIColor yellowColor];
    
    self.TA_LoveView.UIModel = self.userModel;
    
    __block ModuleFirstUserHomepageViewController *root = self;
    self.TA_LoveView.backArrToVC = ^(NSMutableArray *arr){
        
        root.allLoveArray = arr;
        
    };
    
    [self.view addSubview:self.TA_LoveView];
    
    
#pragma mark---喜欢页面点击图片创建通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toTripsPhotoDetails:) name:@"toTripsPhotoDetails" object:nil];
    
    
    [self.travelsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.loveButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
//    self.TA_LoveView.allLoveArray = self.allLoveArray;
    [self.view bringSubviewToFront:self.TA_LoveView];
    
    
    
}

//导航栏左边返回按钮
- (void) leftBackButton{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

#pragma mark--游记页面通知中心要实现的方法
- (void)toTravelsDetails:(NSNotification *)noti{
    
    TravelsDetailsController *MFTDVC = [[TravelsDetailsController alloc]init];
    
    UINavigationController *MFTDNaVC = [[UINavigationController alloc]initWithRootViewController:MFTDVC];
    MFTDVC.idd = [noti object];
    [self presentViewController:MFTDNaVC animated:YES completion:^{
        
        
    }];
    
    
}

#pragma mark--喜欢页面点击图片通知中心要实现的方法
- (void)toTripsPhotoDetails:(NSNotification *)noti{
    
    FirstPhotoDetailsViewController *mFPDVC = [[FirstPhotoDetailsViewController alloc]init];
    mFPDVC.allPhotoArray = self.allLoveArray;
    mFPDVC.selectedPhotoModel = [noti object];
    [self presentViewController:mFPDVC animated:YES completion:^{
        
        
    }];
    
    
}


@end
